package com.winning.util.excelutil.controller;

import com.winning.util.excelutil.common.controller.BaseController;
import com.winning.util.excelutil.common.exception.BaseException;
import com.winning.util.excelutil.service.ExportService;
import com.winning.util.excelutil.util.FileUtil;
import com.winning.util.excelutil.util.TemplateKeys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
 * @作者： caopengfei
 * @时间： 2020/2/12
 */

@RestController
@RequestMapping("export")
@Slf4j
public class ExportController extends BaseController {

    @GetMapping("/template")
    public void downloadTemplates(HttpServletRequest request,HttpServletResponse response){
        String path = request.getSession().getServletContext().getRealPath("/");
        log.error(path);
        File file = new File(path);
        String tomcatPath =  file.getParentFile().getParent();
        String templatePath = tomcatPath+"/template";
        File fileTemplate = new File(templatePath);
        if(fileTemplate.isDirectory()){
       try {
                List<File> files = FileUtil.readFolder(templatePath);
                FileUtil.downLoadFiles(files,request,response);
          } catch (Exception e) {
                throw new BaseException("下载文件失败",e);
            }
        }
    }

}
